<?php

namespace app\agent\controller;

use app\models\{Agent as Agents, Store as Stores, StoreMoneyLog, StoreUser};
use think\Db;
use think\Validate;

class Store extends Base
{

    public function user()
    {
        if ($this->request->isAjax()) {
            $model = new StoreUser();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            if (!empty($post['title'])) {
                $where[] = ['mobile', 'like', '%' . $post['title'] . '%'];
            }
            if (!empty($post['storeid'])) {
                $where[] = ['storeid', '=', $post['storeid']];
            }
            $where[] = ['agentid', '=', $this->request->agent['id']];
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view(null, []);
        }
    }


    public function user_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'mobile' => 'require'
            ], [
                'mobile.require' => '手机号不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            if (!preg_match("/^1[0-9]{10}$/", $data['mobile'])) {
                $this->result([], 1, '手机格式不正确');
            }
            $model = new StoreUser();
            $data['agentid'] = $this->request->agent['id'];
            //查找店铺名称、账号是否已经被使用
            $haveshop = $model->where(['mobile' => $data['mobile'], 'agentid' => $data['agentid']])->find();
            if (!empty($data['id'])) {
                if (!empty($haveshop) && $haveshop['id'] != $data['id']) {
                    return json(['code' => 1, 'data' => [], 'msg' => '账号已存在']);
                }
                $res = $model->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                if (!empty($haveshop)) {
                    return json(['code' => 1, 'data' => [], 'msg' => '账号已存在']);
                }
                $res = $model->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 200, 'data' => [], 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
        } else {
            $model = new StoreUser();
            $id = $this->request->get('id/d', 0);
            $row = $model->where('id', $id)->find();
            return view('', [
                'row' => $row
            ]);
        }
    }


    public function moneylog()
    {
        if ($this->request->isAjax()) {
            $model = new StoreMoneyLog();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $user_id = input('user_id/d') ?? 0;
            $where = array();
            $where[] = ['user_id', '=', $user_id];
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view('', []);
        }
    }

    public function money_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'moneys' => 'require|gt:0',
                'type' => 'require|in:1,2',
                'id' => 'require',
            ], [
                'id.require' => 'ID不能为空',
                'type.require' => '类型不能为空',
                'type.in' => '类型不正确',
                'moneys.require' => '金额不能为空',
                'moneys.gt' => '金额必须大于0',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $model = new StoreUser();
            $user = $model->where(['id' => $data['id'], 'agentid' => $this->request->agent['id']])->find();
            if (empty($user)) {
                return json(['code' => 1, 'data' => [], 'msg' => '用户不存在']);
            }
            $data['desc'] = empty($data['desc']) ? '资产变更' : $data['desc'];
            if ($data['type'] == 1) {
                $money = abs($data['moneys']);
            } else {
                $money = -abs($data['moneys']);
            }
            Db::startTrans();
            try {
                (new Stores())->MoneyEdit($data['id'], $data['type'], $money, $data['desc'], 2);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 1, 'data' => [], 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'data' => [], 'msg' => '操作成功']);
        }
    }
}
